package dao;

import db_util.DBUtil;
import entity.Blog;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ZhangYufei
 * Date: 2021-07-21
 * Time: 13:46
 */
public class BlogDao {
    //新增博客
    public void insert(Blog blog){
        // 1. 和数据库服务器建立连接.
        Connection connection = DBUtil.getConnection();
        // 2. 拼装 SQL
        String sql = "insert into blog(title,content,user_id,postTime) values(?,?,?,now())";
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1,blog.getTitle());
            statement.setString(2,blog.getContent());
            statement.setInt(3,blog.getUserId());
            // 3. 执行 SQL
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 4. 收尾工作.
            DBUtil.close(connection,statement,null);
        }
    }

    //根据博客编号删除博客
    public void delete(int blogId){
        Connection connection = DBUtil.getConnection();
        String sql = "delete from blog where blog_id = ?";
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
    }

    //将所有的博客显示出来
    public List<Blog> selectAll(){
        List<Blog> list = new ArrayList<>();
        Connection connection = DBUtil.getConnection();
        String sql = "select * from blog order by blog_id desc";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blog_id"));
                blog.setTitle(resultSet.getString("title"));
                String content = resultSet.getString("content");
                if(content.length() > 100) {
                    content = content.substring(0,100) + "...";
                }
                blog.setContent(content);
                blog.setUserId(resultSet.getInt("user_id"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                list.add(blog);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return list;
    }

    //显示其中的一篇博客(根据博客id)
    public Blog selectOne(int blogId){
        Connection connection = DBUtil.getConnection();
        String sql = "select * from blog where blog_id = ?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            resultSet = statement.executeQuery();
            if(resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blog_id"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setUserId(resultSet.getInt("user_id"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                return blog;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }

    public static void main(String[] args) {
        // 通过这里的代码对上面的数据库操作进行验证.
        /*// 1. 验证插入.
        // 此处的测试, 只是一个简单的 "冒烟测试"
        Blog blog = new Blog();
        blog.setTitle("这是第一篇博客");
        blog.setContent("这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文这是博客正文");
        blog.setUserId(1);
        BlogDao blogDao = new BlogDao();
        blogDao.insert(blog);*/

        /*// 2. 验证查找.
        BlogDao blogDao = new BlogDao();
        //查找所有博客
        List<Blog> blogs = blogDao.selectAll();
        System.out.println(blogs);
        //查找一篇博客
        Blog blog = blogDao.selectOne(1);
        System.out.println(blog);*/

        /*// 3. 验证删除.
        BlogDao blogDao = new BlogDao();
        blogDao.delete(1);*/
    }
}
